<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * SimplyPost
 *
 * @author		Pascal Kriete
 * @package		SimplyPost
 * @copyright	Copyright (c) 2008, Pascal Kriete
 * @license 	http://www.gnu.org/licenses/lgpl.txt
 */

// ------------------------------------------------------------------------

/**
 * Post Model
 *
 * @package		SimplyPost
 * @subpackage	Post
 * @category	Model
 * @author		Pascal Kriete
 */

class Post_model extends Model {
	
	/**
	 * Constructor
	 *
	 * @access	public
	 */
    function Post_model()
    {
        parent::Model();
    }

	// --------------------------------------------------------------------

	/**
	 * Get parent topic information
	 *
	 * @access	public
	 * @param	integer	parent id
	 * @return	mixed	parent information
	 */
	function get_info($id)
	{
		$this->db->select('id, name, parent_id');
		$this->db->from('topics');
		$this->db->where('id', $id);
		$query = $this->db->get('topics');
		
		if ($query->num_rows < 1)
            return FALSE;
		else
			return $query->result_array();
	}
	
	// --------------------------------------------------------------------
    
	/**
	 * Get all posts in a topic
	 *
	 * @access	public
	 * @param	integer	topic id
	 * @return	mixed
	 */
    function get_posts($id)
    {
		$this->db->where('parent_id', $id);
		$this->db->order_by('created', 'asc');
		$query = $this->db->get('posts');

		if ($query->num_rows < 1)
            return FALSE;
		else
			return $query->result_array();
    }

	// --------------------------------------------------------------------

	/**
	 * Get single post
	 *
	 * @access	public
	 * @param	integer	post id
	 * @return	mixed	post information
	 */
	function get_post($id)
	{
		$this->db->where('id', $id);
		$this->db->select('id, user_id, content');
		$query = $this->db->get('posts');
		
		if ($query->num_rows < 1)
            return FALSE;
		else
			return $query->row_array();
	}
	
	// --------------------------------------------------------------------

	/**
	 * Get newest post
	 *
	 * @access	public
	 * @param	integer	topic id
	 * @return	mixed	post information
	 */
	function get_newest($id)
	{
		$this->db->select('id, parent_id, created, user_id, username');
		$this->db->order_by('created', 'desc');
		$this->db->from('posts');
		$this->db->where('parent_id', $id);
		$query = $this->db->get();
		
		if ($query->num_rows < 1)
            return FALSE;
		else
			return $query->row_array();
	}
	
	// --------------------------------------------------------------------

	/**
	 * Create a new post
	 *
	 * @access	public
	 * @param	mixed	post information
	 * @return	integer
	 */
	function create($data)
	{
		$this->db->insert('posts', $data);
		return $this->db->insert_id();
	}
	
	// --------------------------------------------------------------------
	
	/**
	 * Update Post
	 *
	 * @access	public
	 * @param	mixed	updated post information
	 * @return	void
	 */
	function update($data)
	{
		$this->db->where('id', $data['id']);
		$this->db->update('posts', array('content' => $data['content']));
	}
	
	// --------------------------------------------------------------------
	
	/**
	 * Delete Post
	 *
	 * @access	public
	 * @param	integer	post id
	 * @return	void
	 */
	function delete($id)
	{
		$this->db->delete('posts', array('id' => $id));
	}
}